<template>
  <div class="container">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      center
    >
      <span style="width:100%;text-align: center">{{ msg }}</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
          </span>
    </el-dialog>
  </div>
</template>

<script>
import * as types from '@/store/types'
export default {
  name: 'login',
  data () {
    return {
      dialogVisible: false,
      msg: '',
      form: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    onSubmit () {
      this.$http.get('/login/login', {
        params: {
          username: this.form.username,
          password: this.form.password
        }
      }).then(res => {
        let data = res.data
        if (data.status === 200) {
          this.$store.commit(types.LOGIN, data.data)
          this.$store.commit(types.USERNAME, this.form.username)
          this.$router.push({
            path: '/home'
          })
        } else {
          this.dialogVisible = true
          this.msg = data.msg
        }
      })
    },
    jumpHome () {
      this.$http.get('/login/login', {
        params: {
          username: this.$store.state.username
        }
      }).then(res => {
        let data = res.data
        if (data.status === 200) {
          if (this.$store.state.token !== data.data) {
            this.$store.commit(types.LOGOUT)
          } else {
            this.$store.commit(types.LOGIN, data.data)
            this.$router.push({
              path: '/home'
            })
          }
        }
      })
    }
  },
  mounted () {
    this.jumpHome()
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
.container
  padding 100px 200px 0
  color black

</style>
